package com.i2863.shiroDemo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.i2863.shiroDemo.config.Constant;
import com.i2863.shiroDemo.dto.UserInfo;
import com.i2863.shiroDemo.entity.LoginLog;
import com.i2863.shiroDemo.mapper.LoginLogMapper;
import com.i2863.shiroDemo.util.AddressUtils;
import com.i2863.shiroDemo.service.ILoginLogService;
import org.apache.shiro.session.Session;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @author cexy
 * @date 2020/10/28 14:32
 * @description:
 */
@Service
public class LoginLogServiceImpl extends ServiceImpl<LoginLogMapper, LoginLog> implements ILoginLogService {

    @Override
    @Async("asyncServiceExecutor")
    public void addLoginLog(UserInfo userInfo, Session session) {
        LoginLog loginLog = new LoginLog();
        loginLog.setUserId(userInfo.getId());
        loginLog.setUserName(userInfo.getUserName());
        if (session != null && session.getAttribute(Constant.LOGIN_IP_ADDRESS) != null) {
            loginLog.setIpAddress(session.getAttribute(Constant.LOGIN_IP_ADDRESS).toString());
            loginLog.setGeographyLocation(AddressUtils.getAddressByIp(loginLog.getIpAddress()));
        }
        this.save(loginLog);
    }
}
